<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> {{ myFormTitle }} </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-form ref="searchForm" label-width="80px" :inline="true">
                    <el-form-item label="筛选条件">
                        <el-input placeholder="门店编号、名称" class="handle-input mr10" v-model="query.FilterPara"></el-input>
                    </el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="handleCreate">新建</el-button>
                    <!-- <el-button type="primary" icon="el-icon-folder-add" @click="getFile">上传</el-button> -->
                    <input
                        id="app"
                        ref="uploadExcel"
                        type="file"
                        @change="importfxx(this)"
                        style="display: none;"
                        accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                    />
                </el-form>
            </div>
            <el-table :data="tableData" highlight-current-row style="width: 100%" fit border @selection-change="handleSelectionChange">
                <el-table-column label="区域id" align="center" v-if="show" prop="FAreaID">
                    <template slot-scope="scope">{{ scope.row.FAreaID }}</template>
                </el-table-column>
                <el-table-column label="区域代码" align="center" prop="FAreaNo">
                    <template slot-scope="scope">{{ scope.row.FAreaNo }}</template>
                </el-table-column>
                <el-table-column label="区域名称" align="center" prop="FAreaName">
                    <template slot-scope="scope">{{ scope.row.FAreaName }}</template>
                </el-table-column>
                <el-table-column label="门店代码" align="center" prop=" FShopNo">
                    <template slot-scope="scope">{{ scope.row.FShopNo }}</template>
                </el-table-column>
                <el-table-column label="门店名称" align="center" prop=" FShopName">
                    <template slot-scope="scope">{{ scope.row.FShopName }}</template>
                </el-table-column>
                <el-table-column label="操作" width="250" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.FPageIndex"
                    :page-size="query.FPageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog :title="showDialogTitle" :visible.sync="dialogVisible" width="25%">
            <el-form>
                <el-form-item label="上传json">
                    <input
                        id="upload"
                        type="file"
                        @change="importfxx(this)"
                        accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                    />
                </el-form-item>
            </el-form>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="新建区域" :visible.sync="editVisible" width="30%" @close="formClose">
            <el-form ref="form" :model="form" label-width="100px">
                <!-- <el-form-item>
                    <div class="scaleProgress">
                        <el-progress :text-inside="true" :stroke-width="18" :percentage="progressNum"></el-progress></div
                ></el-form-item> -->
                <el-form-item label="区域名称" prop="FAreaName" :rules="rulesadd.FAreaName">
                    <el-input v-model="form.FAreaName"></el-input>
                </el-form-item>
                <el-form-item label="区域代码" prop="FAreaNo" :rules="rulesadd.FAreaNo">
                    <el-input v-model="form.FAreaNo"></el-input>
                </el-form-item>
                <el-form-item label="涉及门店">
                    <el-select
                        v-model="value"
                        multiple
                        filterable
                        allow-create
                        default-first-option
                        placeholder="请选择门店"
                        style="width:100%"
                    >
                        <el-option v-for="item in options" :key="item.FShopNo" :label="item.FShopName" :value="item.FShopNo"> </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog :title="showDialogTitle" :visible.sync="dialogVisible" width="30%" @close="editFormClose">
            <el-form ref="editForm" :model="editForm" label-width="100px">
                <el-form-item label="区域id" v-if="show"> <el-input v-model="editForm.FAreaID"></el-input> </el-form-item>
                <el-form-item label="区域名称"> <el-input v-model="editForm.FAreaName"></el-input> </el-form-item>
                <el-form-item label="区域代码"> <el-input v-model="editForm.FAreaNo"></el-input> </el-form-item>
                <el-form-item label="涉及门店">
                    <el-select v-model="editValue" @change="currStationChange" style="width:100%">
                        <el-option v-for="item in options" :key="item.FShopNo" :label="item.FShopName" :value="item.FShopNo">
                            <span style="float: left">{{ item.FShopName }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.FShopNo }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchDataFromApi } from '../../api/apiService';
import { Loading } from 'element-ui';
import XLSX from 'xlsx';
export default {
    name: 'basetable',
    data() {
        return {
            progressNum: 10,
            value: '',
            editValue: '',
            show: false,
            currentPage1: 1,
            showDialogTitle: '',
            dialogVisible: false,
            editVisible: false,
            myFormTitle: '区域管理 / 数据清单',
            options: [],
            shopArr: {
                FShopNo: '',
                FShopName: ''
            },
            pageTotal: 0,
            query: {
                FilterPara: '',
                FPageIndex: 1,
                FPageSize: 10
            },
            form: {
                FAreaNo: '',
                FAreaName: '',
                FShopNo: ''
            },
            editForm: {
                FAreaID: '',
                FAreaNo: '',
                FAreaName: '',
                FShopName: '',
                FShopNo: ''
            },
            tableData: [],
            rulesadd: {
                FAreaNo: [{ required: true, message: '请输入区域编号', trigger: 'blur' }],
                FAreaName: [{ required: true, message: '请输入区域名称', trigger: 'blur' }]
            }
        };
    },

    created() {
        this.getData();
        this.getShopData();
    },
    methods: {
        //监听select得变化
        currStationChange(val) {
            this.editValue = val;
            //    console.log(val);
            this.editForm.FShopNo = val;
            var obj = {};
            obj = this.options.find(item => {
                return item.FShopNo === val;
            });
            this.editForm.FShopName = obj.FShopName;
        },
        handleSizeChange(val) {
            // console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            // console.log(`当前页: ${val}`);
        },

        // 触发新增按钮
        handleCreate() {
            this.editVisible = true;
        },
        //表中内容修改时用
        handleSelectionChange(val) {
            this.tableData = val;
        },

        //触发input上传文件
        getFile() {
            this.$refs.uploadExcel.click();
        },

        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'FPageIndex', 1);
            //this.query.FPageIndex = 1;
            this.getData();
        },

        //获取数据
        getData() {
            this.listLoading = true;
            this.query.ActionCode = 'SearchArea';
            this.query.Token = localStorage.getItem('ms_token');
            fetchDataFromApi(this.query).then(res => {
                if (res != undefined && res != null && res != '') {
                    this.tableData = res.data.items;
                    this.pageTotal = res.data.total;
                }
                this.listLoading = false;
            });
        },

        //获取所有的门店信息
        getShopData() {
            this.listLoading = true;
            // this.query.ActionCode = 'SearchShop';
            // this.query.Token = localStorage.getItem('ms_token');
            var myQuery = {};
            myQuery.ActionCode = 'SearchShop';
            myQuery.Token = localStorage.getItem('ms_token');
            myQuery.FilterPara = '';
            myQuery.FPageIndex = 1;
            myQuery.FPageSize = 10000;
            fetchDataFromApi(myQuery).then(res => {
                if (res != undefined && res != null && res != '') {
                    var array = res.data.items;
                    // console.log(res.data.items);
                    for (let i = 0; i < array.length; i++) {
                        this.shopArr = {
                            FShopNo: array[i].FNo,
                            FShopName: array[i].FName
                        };
                        this.options.push(this.shopArr);
                    }
                }
                this.listLoading = false;
            });
        },

        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', { type: 'warning' })
                .then(() => {
                    if (row != undefined && row != null && row != '') {
                        var myRowShopFID = row.FAreaID;

                        var querypars = {};
                        querypars.ActionCode = 'DeleteArea';
                        querypars.Token = localStorage.getItem('ms_token');
                        querypars.FAreaID = myRowShopFID;
                        fetchDataFromApi(querypars).then(res => {
                            if (res != undefined && res != null && res != '') {
                                if (res.Message == 'OK') {
                                    this.$message.success(res.Info);
                                    this.getData();
                                } else {
                                    this.$message.error(res.Info);
                                }
                            }
                        });
                    }
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.showDialogTitle = '编辑区域';
            this.dialogVisible = true;
            this.editForm = Object.assign({}, row);
            this.value = row.FShopName;
            this.editValue = row.FShopName;
        },
        //保存新增
        saveAdd() {
            this.form.FShopNo = this.value.join(',');
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.form.ActionCode = 'AddArea';
                    this.form.Token = localStorage.getItem('ms_token');
                    this.form.FUserNo = localStorage.getItem('ms_userno');
                    fetchDataFromApi(this.form).then(res => {
                        if (res != undefined && res != null && res != '') {
                            // console.log(res);
                            if (res.Message == 'OK') {
                                this.$message.success(res.Info);
                                this.getData();
                            } else {
                                this.$message.error(res.Info);
                            }
                        }

                        this.editVisible = false;
                    });
                }
            });
        },

        //保存编辑
        saveEdit() {
            this.editForm.FShopNo = this.editValue;
            // console.log(JSON.stringify(this.editForm));
            this.$refs.editForm.validate(valid => {
                if (valid) {
                    if (this.editForm.FAreaID == undefined || this.editForm.FAreaID == null || this.editForm.FAreaID == '')
                        this.editForm.ActionCode = 'AddArea';
                    else this.editForm.ActionCode = 'UpdateArea';

                    this.editForm.Token = localStorage.getItem('ms_token');
                    this.editForm.FUserNo = localStorage.getItem('ms_userno');

                    fetchDataFromApi(this.editForm).then(res => {
                        if (res != undefined && res != null && res != '') {
                            if (res.Message == 'OK') {
                                this.$message.success(res.Info);
                                this.getData();
                            } else {
                                this.$message.error(res.Info);
                            }
                        }

                        this.dialogVisible = false;
                    });
                }
            });
        },

        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'FPageIndex', val);
            this.getData();
        },

        handlePageChangeDayTarget(val) {
            this.$set(this.formDay, 'FPageIndex', val);
            this.loadDatTableData();
        },
        formClose() {
            this.$refs.form.clearValidate();

            this.value = '';
            this.form.FCounterName = '';
            this.form.FCounterNo = '';
            // this.$refs['rulesadd'].clearValidate();
        },
        editFormClose() {
            this.editForm = {
                FCounterID: '',
                FCounterName: '',
                FCounterNo: ''
            };
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 220px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>





